//
//  DocumentVC.m
//  Personal Passlock
//
//  Created by 杨岑 on 5/18/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "DocumentVC.h"
#import "PasslockFile.h"


@interface DocumentVC ()

@end

@implementation DocumentVC

- (void)setTitle:(NSString *)title
{
    
}

- (NSArray*)getLayoutConstraints:(NSString*)format views:(NSDictionary*)views
{
    return [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
}

- (void)setup:(NSArray *)items
{
    self.editorView.subviews = @[];
    NSDictionary* columns = @{@"k": @"text", @"v": @"integer", @"ts": @"" };
    NSInteger top = 20;
    for (NSString* name in columns)
    {
        NSTextField* title = [[NSTextField alloc] init];
        title.stringValue = name;
        title.alignment = NSRightTextAlignment;
        title.editable = NO;
        title.bordered = NO;
        title.usesSingleLineMode = YES;
        title.backgroundColor = [NSColor controlColor];
        title.translatesAutoresizingMaskIntoConstraints = NO;
        [self.editorView addSubview:title];
        
        NSTextField* txt = [[NSTextField alloc] init];
        txt.stringValue = columns[name];
        txt.editable = YES;
        txt.usesSingleLineMode = YES;
        txt.translatesAutoresizingMaskIntoConstraints = NO;
        [self.editorView addSubview:txt];
        
        NSMutableArray* constraints = [NSMutableArray array];
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(title, txt);
        //NSLog(@"%@", viewsDictionary);
        
        // Title label
        [constraints addObjectsFromArray:[self getLayoutConstraints:@"|-[title(==80)]" views:viewsDictionary]];
        NSString* s = [NSString stringWithFormat:@"V:|-%ld-[title(==20)]", top];
        [constraints addObjectsFromArray:[self getLayoutConstraints:s views:viewsDictionary]];
        
        // Edit control
        [constraints addObjectsFromArray:[self getLayoutConstraints:@"[title]-[txt]" views:viewsDictionary]];
        [constraints addObjectsFromArray:[self getLayoutConstraints:@"[txt]-|" views:viewsDictionary]];
        s = [NSString stringWithFormat:@"V:|-%ld-[txt(==20)]", top];
        [constraints addObjectsFromArray:[self getLayoutConstraints:s views:viewsDictionary]];
        
        [self.editorView addConstraints:constraints];
        //[self.view.window visualizeConstraints:constraints];
        
        top += 30;
    }
    self.editorHeight.constant = 1000;
}

- (NSDictionary *)getValues
{
    NSMutableDictionary* values = [NSMutableDictionary dictionary];
    return values;
}

@end
